package zhang.lao.extents.spring.handle;/*
 * Copyright (c) 2016 Yee Sight. All Rights Reserved.
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import zhang.lao.console.service.BuildConsoleContentService;
import zhang.lao.pojo.console.ConsoleContext;

import javax.servlet.http.HttpServletRequest;

public class ConsoleContextResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private BuildConsoleContentService buildConsoleContentService;

    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().equals(ConsoleContext.class);
    }

    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        return buildConsoleContentService.build(servletRequest);
    }


}
